package com.ruoyi.common.logistics;

import com.google.gson.Gson;
import com.kuaidi100.sdk.api.*;
import com.kuaidi100.sdk.contant.ApiInfoConstant;
import com.kuaidi100.sdk.contant.CompanyConstant;
import com.kuaidi100.sdk.core.IBaseClient;
import com.kuaidi100.sdk.pojo.HttpResult;
import com.kuaidi100.sdk.request.*;
import com.kuaidi100.sdk.response.QueryTrackMapResp;
import com.kuaidi100.sdk.utils.PropertiesReader;
import com.kuaidi100.sdk.utils.SignUtils;
import com.ruoyi.common.utils.StringUtils;

/**
 * 快递100接入
 */
 public class KuaiDi100 {
        //CGmZaQSF3575
         private String key = PropertiesReader.get("kd100.key");
         //CE2950D29AA7456BFFBFE6C23BDE7429
         private String customer = PropertiesReader.get("kd100.customer");
        //CE2950D29AA7456BFFBFE6C23BDE7429
        private String callbackurl = PropertiesReader.get("kd100.callbackurl");

        /**
         * 实时快递查询接口
         * @param com
         * @param num
         * @param phone
         * @throws Exception
         */
        public HttpResult testQueryTrack(String com,String num,String phone) throws Exception{
                QueryTrackReq queryTrackReq = new QueryTrackReq();
                QueryTrackParam queryTrackParam = new QueryTrackParam();
                queryTrackParam.setCom(com);
                queryTrackParam.setNum(num);
                if(StringUtils.isNotEmpty(phone)){
                        queryTrackParam.setPhone(phone);
                }

                String param = new Gson().toJson(queryTrackParam);

                queryTrackReq.setParam(param);
                queryTrackReq.setCustomer(customer);
                queryTrackReq.setSign(SignUtils.querySign(param ,key,customer));

                IBaseClient baseClient = new QueryTrack();
                HttpResult result=baseClient.execute(queryTrackReq);
                System.out.println();
                return result;
        }

        /**
         * 订阅接口
         * number=物流单号
         * company=CompanyConstant.ST(物流公司)
         */
        public HttpResult subscribeKd100(String number,String company,String phone) throws Exception{
                SubscribeParameters subscribeParameters = new SubscribeParameters();
                subscribeParameters.setCallbackurl(callbackurl);
                subscribeParameters.setPhone(phone);
                SubscribeParam subscribeParam = new SubscribeParam();
                subscribeParam.setParameters(subscribeParameters);
                subscribeParam.setCompany(company);
                subscribeParam.setNumber(number);
                subscribeParam.setKey(key);

                SubscribeReq subscribeReq = new SubscribeReq();
                subscribeReq.setSchema(ApiInfoConstant.SUBSCRIBE_SCHEMA);
                subscribeReq.setParam(new Gson().toJson(subscribeParam));

                IBaseClient subscribe = new Subscribe();
                HttpResult result=subscribe.execute(subscribeReq);
                System.out.println(result);
                return result;
        }
        /**
         * 快递查询地图轨迹
         */
        public void testQueryMapView() throws Exception{
                QueryTrackReq queryTrackReq = new QueryTrackReq();
                QueryTrackParam queryTrackParam = new QueryTrackParam();
                queryTrackParam.setCom(CompanyConstant.YD);
                queryTrackParam.setNum("4311159956248");
                queryTrackParam.setPhone("17725390266");
                queryTrackParam.setFrom("河北保定市");
                queryTrackParam.setTo("湖南岳阳市");
                queryTrackParam.setResultv2("2");
                String param = new Gson().toJson(queryTrackParam);

                queryTrackReq.setParam(param);
                queryTrackReq.setCustomer(customer);
                queryTrackReq.setSign(SignUtils.querySign(param ,key,customer));

                IBaseClient baseClient = new QueryTrackMap();
                HttpResult result = baseClient.execute(queryTrackReq);

                QueryTrackMapResp queryTrackMapResp = new Gson().fromJson(result.getBody(),QueryTrackMapResp.class);
                System.out.println(queryTrackMapResp);
        }
        /**
         * 地图轨迹订阅
         */
        public void testMapSubscribe() throws Exception{
                SubscribeParameters subscribeParameters = new SubscribeParameters();
                subscribeParameters.setCallbackurl("http://www.baidu.com");
                subscribeParameters.setPhone("17725390266");

                SubscribeParam subscribeParam = new SubscribeParam();
                subscribeParam.setParameters(subscribeParameters);
                subscribeParam.setCompany(CompanyConstant.YT);
                subscribeParam.setNumber("YT5319615559666");
                subscribeParam.setFrom("湖北省孝感市大悟县公司");
                subscribeParam.setTo("浙江省宁波市");
                subscribeParam.setKey(key);

                SubscribeReq subscribeReq = new SubscribeReq();
                subscribeReq.setSchema(ApiInfoConstant.SUBSCRIBE_SCHEMA);
                subscribeReq.setParam(new Gson().toJson(subscribeParam));

                IBaseClient subscribe = new SubscribeWithMap();
                System.out.println(subscribe.execute(subscribeReq));
        }

        /**
        * 智能识别接口（正式用户可以使用）
        */
        public void testAutoNum() throws Exception{
                AutoNumReq autoNumReq = new AutoNumReq();
                autoNumReq.setKey(key);
                autoNumReq.setNum("773039762404825");

                IBaseClient baseClient = new AutoNum();
                System.out.println(baseClient.execute(autoNumReq));
        }
 }

